#include "led_matrix.h"

//SET:BSSR   RESET:BRR
void hdl_row_scan(uint8_t row)
{
    switch(row)
    {
        case 0:
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN4;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN3;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN2;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN1;
        break;
        case 1:
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN4;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN3;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN2;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN1;
        break;
        case 2:
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN4;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN3;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN2;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN1;
        break;
        case 3:
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN4;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN3;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN2;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN1;
        break;
        case 4:
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN4;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN3;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN2;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN1;
        break;
        case 5:
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN4;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN3;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN2;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN1;
        break;
        case 6:
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN4;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN3;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN2;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN1;
        break;
        case 7:
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN4;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN3;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN2;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN1;
        break;
        case 8:
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN4;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN3;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN2;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN1;
        break;
        case 9:
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN4;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN3;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN2;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN1;
        break;
        case 10:
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN4;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN3;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN2;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN1;
        break;
        case 11:
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN4;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN3;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN2;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN1;
        break;
        case 12:
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN4;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN3;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN2;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN1;
        break;
        case 13:
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN4;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN3;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN2;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN1;
        break;
        case 14:
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN4;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN3;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN2;
            LED_MATRIX_ROW_PORT->BRR = LED_MATRIX_ROW_PIN1;
        break;
        case 15:
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN4;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN3;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN2;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN1;
        break;
        default:
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN4;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN3;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN2;
            LED_MATRIX_ROW_PORT->BSRR = LED_MATRIX_ROW_PIN1;
        break;
    }
}

void hdl_colnumn_send_data(uint8_t dat)
{
    uint8_t i = 0;
    LED_MATRIX_COLNUMN_PORT->BRR = LED_MATRIX_COLNUMN_SH_CP;
    systick_delay_us(4);
    for ( i = 0; i < 8; i++)
    {
        if(dat & 0x1)
        {
            GPIO_SetBits(LED_MATRIX_COLNUMN_PORT, LED_MATRIX_COLNUMN_DS);
        }
        else
        {
            GPIO_ResetBits(LED_MATRIX_COLNUMN_PORT, LED_MATRIX_COLNUMN_DS);
        }
        GPIO_SetBits(LED_MATRIX_COLNUMN_PORT, LED_MATRIX_COLNUMN_SH_CP);
        systick_delay_us(2);
        GPIO_ResetBits(LED_MATRIX_COLNUMN_PORT, LED_MATRIX_COLNUMN_SH_CP);
        systick_delay_us(2);
        dat >>= 1;
    }
    
}


void hdl_screen_full_check(void)
{

}

    

int8_t hdl_led_matrix_init(void)
{
    int8_t ret = SUCCESS;
    GPIO_InitTypeDef    GPIOInitStructre;
    LED_MATRIX_GPIO_PeriphClockFunc(LED_MATRIX_GPIO_PeriphClock,ENABLE);
    GPIOInitStructre.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIOInitStructre.GPIO_Pin = LED_MATRIX_ROW_PIN1|LED_MATRIX_ROW_PIN2|LED_MATRIX_ROW_PIN3|LED_MATRIX_ROW_PIN4;
    GPIOInitStructre.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(LED_MATRIX_ROW_PORT, &GPIOInitStructre);


    LED_MATRIX_COLNUMN_PeriphClockFunc(LED_MATRIX_COLNUMN_PeriphClock,ENABLE);
    GPIOInitStructre.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIOInitStructre.GPIO_Pin = LED_MATRIX_COLNUMN_DS|LED_MATRIX_COLNUMN_ST_CP|LED_MATRIX_COLNUMN_SH_CP|LED_MATRIX_COLNUMN_MR;
    GPIOInitStructre.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(LED_MATRIX_COLNUMN_PORT, &GPIOInitStructre);

    LED_MATRIX_COLNUMN_PORT->BRR = LED_MATRIX_COLNUMN_DS;
    LED_MATRIX_COLNUMN_PORT->BRR = LED_MATRIX_COLNUMN_ST_CP;
    LED_MATRIX_COLNUMN_PORT->BRR = LED_MATRIX_COLNUMN_SH_CP;
    LED_MATRIX_COLNUMN_PORT->BRR = LED_MATRIX_COLNUMN_MR;
    hdl_screen_full_check();

    return ret;
}


 

